package cn.edu.tyu.finance.thingstype.dao;

import cn.edu.tyu.util.DbUtil;
import cn.edu.tyu.finance.thingstype.domain.Users;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDaoImpl {
    public int addUser(Users users) throws SQLException {
        //获取到数据库连接
        Connection conn = DbUtil.getConnection();
        //编写sql语句
        String sql = "insert into users values(?,?,?)";
        //获取预编译语句的执行者
        PreparedStatement ps = conn.prepareStatement(sql);
        //设置参数
        ps.setString(1, users.getUserid());
        ps.setString(2, users.getUsername());
        ps.setString(3, users.getUserpsw());
        //执行sql语句
        int res = ps.executeUpdate();
        //释放数据库的连接
        DbUtil.backConn(conn, ps, null);
        return res;
    }

    /**
     * *测试类
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {
        UserDaoImpl userDao = new UserDaoImpl();
        Users users = new Users();
        users.setUserid("1");
        users.setUsername("zs");
        users.setUserpsw("123456");
        try {
            int res = userDao.addUser(users);
            System.out.print("res=" + res);
        } catch (SQLException e) {

        }
    }
}

